package com.intel.two.service.community.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.mapper.community.PetMapper;
import com.intel.two.pojo.community.Pet;
import com.intel.two.service.community.PetService;
import com.intel.two.vo.community.petBarVo;
import com.intel.two.vo.community.petPieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service("petService")
public class PetServiceImpl implements PetService {
    @Autowired
    private PetMapper petMapper;
    @Override
    public LayuiPageVo<Pet> petPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<Pet> pets =  petMapper.selectByParams(params);

        /*分页*/
        PageInfo<Pet> petPageInfo = new PageInfo<>(pets);
        LayuiPageVo<Pet> petLayuiPageVo = new LayuiPageVo<>();
        petLayuiPageVo.setCode(0);
        petLayuiPageVo.setMsg("学生分页列表数据");
        petLayuiPageVo.setCount(petPageInfo.getTotal());
        petLayuiPageVo.setData(petPageInfo.getList());
        return petLayuiPageVo;
    }

    @Override
    public int addPet(Pet pet) {
        return petMapper.addPet(pet);
    }

    @Override
    public int deletePet(Integer pet_id) {
        return petMapper.deletePet(pet_id);
    }

    @Override
    public int deletePetAll(Map<String, Object> map) {
        String str = (String) map.get("pet_id");
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            System.out.println(strs[i]);
        }
        return petMapper.deletePetAll(list);
    }

    @Override
    public int updatePet(Pet pet) {
        return petMapper.updatePet(pet);
    }

    @Override
    public List<petBarVo> petBar() {
        return petMapper.petBar();
    }

    @Override
    public List<petPieVo> petPie() {
        return petMapper.petPie();
    }
}
